package unstruct

import (
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/util/json"
)

func ObjectToUnstruct(obj runtime.Object) *unstructured.Unstructured {
	byt, err := json.Marshal(obj)
	if err != nil {
		return nil
	}
	m := make(map[string]interface{})
	err = json.Unmarshal(byt, &m)
	if err != nil {
		return nil
	}

	return &unstructured.Unstructured{Object: m}
}

func UnstructToObject(data *unstructured.Unstructured, obj runtime.Object) error {
	return runtime.DefaultUnstructuredConverter.FromUnstructured(data.UnstructuredContent(), obj)
}
